package com.mq.agency.mq;

import com.mq.agency.server.ProxyServerHandler;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import org.apache.commons.lang3.SerializationUtils;

import java.io.IOException;

/**
 * @description:
 * @author: zzb
 * @time: 2021/12/28 11:21
 */
public class MyConsumer extends DefaultConsumer {
    public MyConsumer(Channel channel) {
        super(channel);
    }

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        Object deserialize = SerializationUtils.deserialize(body);
        System.out.println(deserialize);
        Object o = ProxyServerHandler.stationMap.get(envelope.getRoutingKey());
        //手动确认消息,一次确认一个
//        super.getChannel().basicAck(envelope.getDeliveryTag(),false);
        if (o!=null){
            io.netty.channel.Channel channel = (io.netty.channel.Channel) o;
            channel.writeAndFlush(body);
        }
//        super.getChannel().basicAck(envelope.getDeliveryTag(),false);
    }
}
